package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Coursetalk;
import com.hs.sfzy.service.CoursetalkService;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CoursetalkController
 * @Description TODO
 * @Author zgy
 * @Date 2024年10月25日 10:46
 * @Version 1.0
 */
@RestController
@RequestMapping("/coursetalk")
public class CoursetalkController {
    @Autowired
    private CoursetalkService coursetalkService;

    /**
     * 根据id删除
     *
     * @param id
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/25 10:47
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        int num = coursetalkService.delete(id);
        if (num != 0) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }


    /**
     * 查询所有数据
     *
     * @param coursetalk
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/25 10:48
     */
    @GetMapping
    public Result selectAll(Coursetalk coursetalk) {
        List<Coursetalk> list = coursetalkService.selectAll(coursetalk);
        return Result.success(list);
    }

    /**
     * 根据id查询数据
     * @author zgy
     * @date 2024/10/25 10:50
     * @param id
     * @return com.hs.sfzy.util.Result
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Long id) {
        Coursetalk coursetalk = coursetalkService.selectById(id);
        return Result.success(coursetalk);
    }

    /**
     * 分页查询数据
     * @author zgy
     * @date 2024/10/25 10:50
     * @param pageNum
     * @param pageSize
     * @param coursetalk 
     * @return com.hs.sfzy.util.Result
     */
    @GetMapping("/findByPage")
    public Result selectByPage(
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "5") int pageSize,
            Coursetalk coursetalk) {
        PageInfo<Coursetalk> info = coursetalkService.selectByPageInfo(pageNum, pageSize, coursetalk);
        return Result.success(info);
    }

    @GetMapping("/findByCourseId")
    public Result selectByCourseId(Long courseId){
        List<Coursetalk> coursetalks = coursetalkService.selectByCourseId(courseId);
        return Result.success(coursetalks);
    }

    @PostMapping("/add")
    public Result insert(@RequestBody Coursetalk coursetalk){
        int num = coursetalkService.insert(coursetalk);
        if (num != 0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }
}
